#!/bin/bash

# 加载环境变量
source ./build_env.sh

# 设置目录
INSTALL_DIR=$BUILD_DIR/zlib
FRAMEWORK_DIR="$BUILD_DIR/framework"
FRAMEWORK_NAME="zlib"
FRAMEWORK_PATH="$FRAMEWORK_DIR/$FRAMEWORK_NAME.framework"

# 确保universal库已经构建
if [ ! -f "$INSTALL_DIR/universal/lib/libz.a" ]; then
    echo "错误：通用库不存在，请先运行build_zlib_static.sh"
    exit 1
fi

# 创建framework目录结构
rm -rf "$FRAMEWORK_PATH"
mkdir -p "$FRAMEWORK_PATH/Headers"
mkdir -p "$FRAMEWORK_PATH/Modules"

# 复制头文件 - zlib只有几个头文件
cp "$INSTALL_DIR/universal/include/zconf.h" "$FRAMEWORK_PATH/Headers/"
cp "$INSTALL_DIR/universal/include/zlib.h" "$FRAMEWORK_PATH/Headers/"

# 直接复制universal库文件到framework
# 由于这是一个简单的库，我们不需要合并多个库文件
cp "$INSTALL_DIR/universal/lib/libz.a" "$FRAMEWORK_PATH/$FRAMEWORK_NAME"

# 创建Info.plist文件
cat > "$FRAMEWORK_PATH/Info.plist" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>$FRAMEWORK_NAME</string>
    <key>CFBundleIdentifier</key>
    <string>org.zlib</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$FRAMEWORK_NAME</string>
    <key>CFBundlePackageType</key>
    <string>FMWK</string>
    <key>CFBundleShortVersionString</key>
    <string>1.3.1</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>CFBundleSupportedPlatforms</key>
    <array>
        <string>iPhoneOS</string>
        <string>iPhoneSimulator</string>
    </array>
    <key>MinimumOSVersion</key>
    <string>$IPHONEOS_DEPLOYMENT_TARGET</string>
</dict>
</plist>
EOF

# 创建模块映射文件
cat > "$FRAMEWORK_PATH/Modules/module.modulemap" << EOF
framework module zlib {
    umbrella header "zlib.h"
    export *
    module * { export * }
}
EOF

echo "zlib.framework 已成功创建在 $FRAMEWORK_PATH"
echo "您可以将此框架拖入您的Xcode项目中使用"